package com.runa.monitor.platinterface.extend.third.newester.controller;


import com.alibaba.fastjson.JSONObject;
import com.runa.monitor.platinterface.extend.third.newester.pojo.AjaxResponse;
import com.runa.monitor.platinterface.extend.third.newester.pojo.ResponseCode;
import com.runa.monitor.platinterface.extend.third.newester.service.EquDockingDataService;
import com.runa.monitor.platinterface.extend.third.newester.vo.*;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;

@RestController
@RequestMapping("comm")
public class EquDockingDataController {

    private static final int MAX_SIZE = 300;

    private static final Logger logger = LoggerFactory.getLogger(EquDockingDataController.class);

    @Resource
    private EquDockingDataService equDockingDataService;

    @PostMapping("houseMeterData")
    public AjaxResponse houseMeterData(@RequestBody DockingDataParentVo<HouseMeterDockingDetailVo> dockingDataParentVo) {
        logger.info("收到的户表原始数据为：{}", JSONObject.toJSONString(dockingDataParentVo));
        List<HouseMeterDockingDetailVo> detailVoList = dockingDataParentVo.getCurrentDataInfo();
        if (CollectionUtils.isEmpty(detailVoList) || detailVoList.size() > MAX_SIZE) {
            return AjaxResponse.of(ResponseCode.LIMIT_DATA_300, "户表设备数据条数为0-" + MAX_SIZE);
        }
        return equDockingDataService.saveHouseMeterData(detailVoList);
    }


    @PostMapping("houseValveData")
    public AjaxResponse houseValveData(@RequestBody DockingDataParentVo<HouseValveDockingDetailVo> dockingDataParentVo) {
        logger.info("收到的户阀原始数据为：{}", JSONObject.toJSONString(dockingDataParentVo));
        List<HouseValveDockingDetailVo> detailVoList = dockingDataParentVo.getCurrentDataInfo();
        if (CollectionUtils.isEmpty(detailVoList) || detailVoList.size() > MAX_SIZE) {
            return AjaxResponse.of(ResponseCode.LIMIT_DATA_300, "户阀设备数据条数为0-" + MAX_SIZE);
        }
        return equDockingDataService.saveHouseValveData(detailVoList);
    }


    @PostMapping("temperaturePanelData")
    public AjaxResponse temperaturePanelData(@RequestBody DockingDataParentVo<TemperaturePanelDockingDetailVo> dockingDataParentVo) {
        logger.info("收到的温度面板原始数据为：{}", JSONObject.toJSONString(dockingDataParentVo));
        List<TemperaturePanelDockingDetailVo> detailVoList = dockingDataParentVo.getCurrentDataInfo();
        if (CollectionUtils.isEmpty(detailVoList) || detailVoList.size() > MAX_SIZE) {
            return AjaxResponse.of(ResponseCode.LIMIT_DATA_300, "温度面板设备数据条数为0-" + MAX_SIZE);
        }
        return equDockingDataService.saveTemperaturePanelData(detailVoList);
    }

    @PostMapping("temperaturePressureData")
    public AjaxResponse temperaturePressureData(@RequestBody DockingDataParentVo<TemperaturePressureDockingDetailVo> dockingDataParentVo) {
        logger.info("收到的温压监测仪原始数据为：{}", JSONObject.toJSONString(dockingDataParentVo));
        List<TemperaturePressureDockingDetailVo> detailVoList = dockingDataParentVo.getCurrentDataInfo();
        if (CollectionUtils.isEmpty(detailVoList) || detailVoList.size() > MAX_SIZE) {
            return AjaxResponse.of(ResponseCode.LIMIT_DATA_300, "温压监测仪设备数据条数为0-" + MAX_SIZE);
        }
        return equDockingDataService.saveTemperaturePressureData(detailVoList);
    }

    @PostMapping("netequcurrentdata")
    public AjaxResponse netequcurrentdata(@RequestBody DockingDataParentVo<NetEquDockingDetailVo> dockingDataParentVo) {
        logger.info("收到的网络设备原始数据为：{}", JSONObject.toJSONString(dockingDataParentVo));
        List<NetEquDockingDetailVo> detailVoList = dockingDataParentVo.getCurrentDataInfo();
        if (CollectionUtils.isEmpty(detailVoList) || detailVoList.size() > MAX_SIZE) {
            return AjaxResponse.of(ResponseCode.LIMIT_DATA_300, "网络设备数据条数为0-" + MAX_SIZE);
        }
        return equDockingDataService.saveNetEquData(detailVoList);
    }


    @PostMapping("stationMeterData")
    public CheckResultList addStationMeterCurrentData(@RequestBody StationMeterListVO in,
                                                      HttpServletRequest request, HttpServletResponse response) {
        CheckResultList result = new CheckResultList();
        if(in.getCurrentDataInfo() != null && in.getCurrentDataInfo().size()>300){
            result.setCode(ResponseCode.LIMIT_DATA_300.code());
            return result;
        }
        try {
            result = equDockingDataService.saveStationMeterData(in);
        } catch (Exception e) {
            result.setCode(ResponseCode.FAIL.code());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            e.printStackTrace(new PrintStream(baos));
            String exception = baos.toString();
            logger.info(" stationMeterCurrentData System erro :"+exception);
        }
        return result;
    }
}
